package com.unknown

import org.eclipse.jetty.websocket.WebSocketServlet
import org.apache.activemq.transport.TransportAcceptListener
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import org.eclipse.jetty.websocket.WebSocket

/**
 * Created by IntelliJ IDEA.
 * User: propatalent
 * Date: 10/27/11
 * Time: 12:31 AM
 * To change this template use File | Settings | File Templates.
 */
class StompServlet extends WebSocketServlet {

    private TransportAcceptListener listener;

    public void init() throws ServletException {
        super.init();
        listener = (TransportAcceptListener)getServletContext().getAttribute("acceptListener");
        if (listener == null) {
            throw new ServletException("No such attribute 'acceptListener' available in the ServletContext");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException ,IOException  {
        getServletContext().getNamedDispatcher("default").forward(request,response);
    }

    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
        StompSocket socket = new StompSocket();
        listener.onAccept(socket);
        return socket;
    }
}
